package com.luo.shop.service.impl;

import com.luo.shop.dao.CartDao;
import com.luo.shop.dao.GoodsBoDao;
import com.luo.shop.dao.GoodsDao;
import com.luo.shop.pojo.Cart;
import com.luo.shop.pojo.Goods;
import com.luo.shop.pojo.User;
import com.luo.shop.service.CartService;
import jdk.nashorn.internal.runtime.regexp.joni.constants.CCSTATE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.rmi.server.UID;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartDao cartDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsBoDao goodsBoDao;

    public List<Cart> queryByGidUid(Long gid, Long uid) {
        Example example = new Example(Cart.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("createtime desc");
        criteria.andEqualTo("uid",uid);
        example.and(criteria);
        List<Cart> list = cartDao.selectByExample(example);
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        for (Cart cart : list) {
            Goods goods = goodsDao.selectByPrimaryKey(cart.getGid());
            goods.setGoodsBoOne(goodsBoDao.selectByPrimaryKey(cart.getGoodsBoId()));
            cart.setGoods(goods);
        }


        return list;
    }

    public Integer save(Cart cart) {

        Cart delCar = new Cart();
        delCar.setUid(cart.getUid());
        delCar.setGid(cart.getGid());
        cartDao.delete(delCar);
        cart.setIsComment(false);
        cart.setIsPayment(false);
        cart.setCreatetime(new Date());
        cart.setIsReceiving(false);
        cartDao.insertSelective(cart);

        return Integer.parseInt(cart.getId()+"");
    }
    @Transactional
    public Integer del(String id, Long uid) {
        Integer len = 0;
        if (id.indexOf(",") == -1) {
           Cart cart = new Cart();
           cart.setIsPayment(true);
           cart.setId(Long.parseLong(id));
            len = cartDao.updateByPrimaryKeySelective(cart);
            if (len>0){
                cart = cartDao.selectByPrimaryKey(Long.parseLong(id));
                Goods goods = goodsDao.selectByPrimaryKey(cart.getGid());
                goods.setRepertory(goods.getRepertory()-cart.getOrderNumber());
                goodsDao.updateByPrimaryKeySelective(goods);
            }
        } else {
            String[] split = id.split(",");
            for (String s : split) {
                Cart cart = new Cart();
                cart.setIsPayment(true);
                cart.setId(Long.parseLong(s));
                len =  cartDao.updateByPrimaryKeySelective(cart);
                if (len>0){
                    cart = cartDao.selectByPrimaryKey(Long.parseLong(id));
                    Goods goods = goodsDao.selectByPrimaryKey(cart.getGid());
                    goods.setRepertory(goods.getRepertory()-cart.getOrderNumber());
                    goodsDao.updateByPrimaryKeySelective(goods);
                }
            }
        }
        return len;
    }

    public Integer upd(Cart cart) {
        return cartDao.updateByPrimaryKeySelective(cart);
    }

    @Transactional
    public Integer delete(String id) {
        Integer len = 0;
        if (id.indexOf(",") == -1) {
            len = cartDao.deleteByPrimaryKey(Long.parseLong(id));
        } else {
            String[] split = id.split(",");
            for (String s : split) {
              len =  cartDao.deleteByPrimaryKey(Long.parseLong(s));
            }
        }
        return len;
    }
}
